package com.dnzx.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.dnzx.framework.page.PageResult;
import org.dnzx.framework.result.ResultUtils;
import org.dnzx.framework.result.domain.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnzx.client.IUserClient;
import com.dnzx.model.system.UserBO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RequestMapping("/user")
@RestController
@Api(value = "/user", description = "用户管理", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

	@Resource
	private IUserClient userClient;
	
	/**
	 * 新增
	 * @param userBO
	 * @return
	 */
	@PostMapping(value = "/save")
    @ApiOperation(value = "/save",notes = "新增:name、sex、mobile、idCard",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result save(@ApiParam(value = "实体") @RequestBody @Valid UserBO userBO){
		try{
			this.userClient.save(userBO);
	        return ResultUtils.returnSuccess("SUCCESS");
        }catch (Exception e){
            return ResultUtils.returnError("ERROR >>> " + e.getMessage());
        }
    }
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete/{id}")
	@ApiOperation(value = "/delete/{id}",notes = "删除",httpMethod = "DELETE",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result delete(@ApiParam(value = "id") @PathVariable("id") Long id){
		try {
			this.userClient.delete(id);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 修改
	 * @param userBO
	 * @return
	 */
	@PutMapping(value = "/update")
	@ApiOperation(value = "/update",notes = "修改:id、name、sex、mobile、idCard",httpMethod = "PUT",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result update(@ApiParam(value = "实体") @RequestBody UserBO userBO) {
		try {
			this.userClient.update(userBO); 
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
     * 修改密码
     * @param user
     * @param request
     * @return
     */
    @PutMapping(value = "/modifyPwd")
    @ApiOperation(value = "/modifyPwd", notes = "修改密码：password、newPassword，不需要传确认密码", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result modifyPwd(@RequestBody UserBO userBO, HttpServletRequest request) {
        try {
        	String langCode = request.getHeader("langCode");
            this.userClient.updatePwd(userBO, langCode);
            return ResultUtils.returnSuccess("SUCCESS");
        } catch (Exception e) {
            return ResultUtils.returnError("ERROR >>> " + e.getMessage());
        }
    }
	
	/**
	 * 查看
	 * @param id
	 * @return
	 */
	@GetMapping("/get/{id}")
	@ApiOperation(value = "/get/{id}",notes = "查看",httpMethod = "GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result get(@ApiParam(value = "id") @PathVariable("id") Long id) {
		try {
			UserBO userBO = this.userClient.get(id);
			return ResultUtils.returnSuccess("SUCCESS", userBO);
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}

	/**
	 * 查询 - 分页
	 * @param userBO
	 * @return
	 */
	@PostMapping("/queryPage")
	@ApiOperation(value="/queryPage",notes="查询 - 分页:mobile",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryPage(@ApiParam(value = "实体") @RequestBody UserBO userBO) {
		try {
			PageResult<List<UserBO>> pageResult = new PageResult<List<UserBO>>();
			pageResult = this.userClient.queryPage(userBO, pageResult);
			return ResultUtils.returnSuccess("SUCCESS", pageResult);
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 查询 - 列表
	 * @param userId
	 * @return
	 */
	@GetMapping("/queryList")
	@ApiOperation(value="/queryList",notes="查询 - 列表",httpMethod="GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryList() {
		try {
			List<UserBO> userBOList = this.userClient.queryList(new UserBO());
			return ResultUtils.returnSuccess("SUCCESS", userBOList);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
}
